/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.workflowMonitoring.controller;

import com.koron.workflow.common.annotation.CaseUser;
import com.koron.workflow.common.bean.PageBean;
import com.koron.workflow.common.bean.StaffBean;
import com.koron.workflow.common.util.BusinessCode;
import com.koron.workflow.workflowMonitoring.service.WorkflowMonitoringService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(value = "workflowMonitoring", description = "工作流监控")
@RestController
@RequestMapping("/workflowMonitoring")
public class WorkflowMonitoringController {

    @Autowired
    private WorkflowMonitoringService service;

    private static final Logger logger = LoggerFactory.getLogger(WorkflowMonitoringController.class);


    @ApiOperation("查询用户有监控权限的模版组")
    @GetMapping("/queryMonitoringProcessList.htm")
    public MessageBean queryMonitoringProcessList(@ApiIgnore @CaseUser StaffBean user) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        try {
            List<Map> list = ADOConnection.runTask(service, "queryMonitoringProcessList", List.class, user);
            msg.setData(list);
        } catch (Exception e) {
            logger.error("查询用户有监控权限的模版组失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

    @ApiOperation("模版分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用ID", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "setId", value = "所属模板组ID", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "模板编号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "模板名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "标题", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, dataType = "int", paramType = "query"),
    })
    @GetMapping("queryForList.htm")
    public MessageBean queryForList(@ApiIgnore @CaseUser StaffBean user, @ApiIgnore String appId, @ApiIgnore String setId,
                                    @ApiIgnore String code, @ApiIgnore String name, @ApiIgnore String title,
                                    @ApiIgnore Integer page, @ApiIgnore Integer pageSize) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        if (page != null) {
            page = page - 1;
            if (page < 0) {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("页码不能小于1");
                return msg;
            }
        }
        try {
            PageBean pageBean = ADOConnection.runTask(service, "queryForList", PageBean.class,
                    user, appId, setId, code, name, title, page * pageSize, pageSize);
            msg.setData(pageBean);
        } catch (Exception e) {
            logger.error("分页查询模版失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processInstanceId", value = "流程实例Id", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "/delete.htm")
    public MessageBean delete(@ApiIgnore String processInstanceId) {
        return ADOConnection.runTask(service, "delete", MessageBean.class, processInstanceId);
    }


    @ApiOperation("结束")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tasklistId", value = "任务列表ID", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "/endTask.htm")
    public MessageBean endTask(@ApiIgnore String tasklistId) {
        return ADOConnection.runTask(service, "endTask", MessageBean.class, tasklistId);
    }

    @ApiOperation("查询当前节点及之前的历史节点信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curTaskId", value = "当前任务id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "processInstanceId", value = "流程实例Id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping(value = "/queryHisNodeMsgs.htm")
    public MessageBean queryHisNodeMsgs(@CaseUser StaffBean user, @ApiIgnore String curTaskId, @ApiIgnore String processInstanceId) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        try {
            // 查询当前节点及之前的历史节点信息
            List hisNodeMsgs = ADOConnection.runTask(service, "queryHisNodeMsgs", List.class, curTaskId, processInstanceId);
            // 根据流程实列ID 获取要进行驳回参数信息
            Map map = ADOConnection.runTask(service, "getRejectParams", Map.class, processInstanceId);
            map.put("hisNodeMsgs", hisNodeMsgs);
            map.put("operator", user.getAccount());
            map.put("taskId", curTaskId);
            map.put("operation", "1");
            map.put("rejectNodeCode", "");
            msg.setData(map);
        } catch (Exception e) {
            logger.error("查询当前节点及之前的历史节点信息失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }
}
